/*
 * CloudBank Mobile Knowledge Sharing for Language Learners
 * Supported by the JISC Rapid Innovation Programme #jiscri
 *
 * Copyright (C) 2010 University of Brighton
 *
 * Licensed under the GNU General Public License v3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package uk.ac.brighton.itrg.cloudbank;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ActivityHyperlink extends Activity implements OnClickListener
{
	private static final String TAG = ActivityHyperlink.class.getSimpleName();

	private WebView mBrowser;

	// intercept redirects etc. to avoid new browser windows	
	final class MyWebViewClient extends WebViewClient
	{
		public boolean shouldOverwriteUrlLoading(WebView view, String url)
		{
	        mBrowser.loadUrl(url);
			return true;
		}
	}
	
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
        Logger.log(TAG + ".onCreate()");
        try
        {
            setContentView(R.layout.hyperlink);

	        findViewById(R.id.btnOk).setOnClickListener(this);
	        findViewById(R.id.btnCancel).setOnClickListener(this);

	        mBrowser = ((WebView)findViewById(R.id.browser));
	        mBrowser.getSettings().setJavaScriptEnabled(true);
	        mBrowser.setWebViewClient(new MyWebViewClient());
	        
	        String url = CB.getSearchEngine();
	        if(getIntent().hasExtra("url"))
	        	url = getIntent().getExtras().getString("url");
	        
	        
	        mBrowser.loadUrl(url);
	        Logger.log("mBrowser.loadUrl(url)", url);
        }
        catch(Exception e)
        {
        	Logger.log(e);
        }
	}
	

	// ------------------------------------------
    // app becomes visible
	// ------------------------------------------
	public void onStart()
	{
		super.onStart();
        Logger.log(TAG + ".onStart()");
	}

	// ------------------------------------------
    // app becomes invisible
	// ------------------------------------------
	public void onStop()
	{
		super.onStop();
        Logger.log(TAG + ".onStop()");
	}

	
	// ------------------------------------------
    // interface OnClickListener
	// ------------------------------------------
    public void onClick(View v) 
    {
        Logger.log(TAG + ".onClick()");

    	if(v.getId() == R.id.btnOk)
        {
    		Intent data = new Intent();
    		data.putExtra("url", mBrowser.getUrl());
    		setResult(RESULT_OK, data);
        } 
    	else 
        {
    		setResult(RESULT_CANCELED);
        }
		finish();
    }
}
